package cz.czu.pef.DataModelling.entityObjects;

import java.util.*;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * This entity represents concrete employee role in the company (role instance). RoleEntityImpl itself is defined by related RoleTypeEntityImpl.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name="ROLE")
public class RoleEntityImpl extends IdPersistenceEntityObjectImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = 8392953561903669343L;

	/*
	 * Entity attributes
	 */

	@ManyToOne(fetch = FetchType.EAGER, targetEntity=RoleTypeEntityImpl.class, optional=false) // no cascade
	private RoleTypeEntityImpl roleType;

	/*
	 * Relations
	 */
	@ManyToMany(fetch = FetchType.EAGER, targetEntity = EmployeeEntityImpl.class, mappedBy = "roles") // no cascade
	private Set<EmployeeEntityImpl> employees;

	public RoleEntityImpl()
	{
		super();
	}

	public RoleEntityImpl(RoleTypeEntityImpl roleType)
	{
		super();
		this.roleType = roleType;
	}

	public RoleTypeEntityImpl getRoleType()
	{
		return roleType;
	}

	public void setRoleType(RoleTypeEntityImpl roleType)
	{
		this.roleType = roleType;
	}

	public Set<EmployeeEntityImpl> getEmployees()
	{
		return employees;
	}

	public List<EmployeeEntityImpl> getEmployeesAsList()
	{
		return new ArrayList<EmployeeEntityImpl>(getEmployees());
	}

    
  public void setEmployees(Set<EmployeeEntityImpl> employees)
  {
    this.employees = employees;
  }

    @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("RoleEntityImpl [roleType=");
		builder.append(roleType);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}

	/* (non-Javadoc)
	 * @see cz.czu.pef.DataModelling.domainObjects.Role#canBeRemoved()
	 */
	@Override
	public boolean canBeRemoved()
	{
		return true;
	}
}
